










//<script>

var _prjs_S2P;
var _tmr_S2P;
/**
	this function loads all the activities initially
*/
function _prjS2P(){
	this.id; //activity start time
	this.name; // activity name
	this.pt; // last punch time
	this.tot=0; // total time worked
	this.ty='o'; //last punch type in or out.
	this.log=''; // time o|time i|
}
function _aprjS2P(){
	var d = $(_mPdS2P('afd'));
	d.style.display='';
	//;
}
/**
	_nprjS2P creates a new activity and sets the punch type to out
*/
function _nprjS2P(frm){
	var p = new _prjS2P();
	if(frm.name.value.indexOf(':') >=0){
		alert("Activity name contains invalid character ':'");
		return;
	}
	if(frm.name.value.indexOf('>') >=0){
		alert("Activity name contains invalid character '>'");
		return;	
	}
	p.name = escape(frm.name.value);	
	p.tot = 0;
	p.ty = 'o';	
	p.id = (new Date()).getTime();
	if(!_prjs_S2P) _prjs_S2P = new Array();
	_prjs_S2P[_prjs_S2P.length] = p;
	_sAveS2P();
	var d = $(_mPdS2P('afd'));
	d.style.display='none';	
	_rNDrS2P();
	//;
	frm.name.value = '';	
	
}
function _adcanS2P(){
	var d = $(_mPdS2P('afd'));
	d.style.display='none';	
	//;
}
function _gPrjS2P(pid){
	if(_prjs_S2P && _prjs_S2P.length >0){
		for(i=0; i< _prjs_S2P.length; i++){
			if(_prjs_S2P[i].id == pid){
				return _prjs_S2P[i];
			}
		}
	}
	return null;
}
/**
	_pinS2P - punch in function. This function updates the lastpunch time and 
	type to i and updates the logs.
*/
function _pinS2P(pid){
	var p;
	// punchout the current punched in activity
	if(_prjs_S2P && _prjs_S2P.length >0){
		for(i=0; i< _prjs_S2P.length; i++){
			if(_prjs_S2P[i].id == pid){
				p = _prjs_S2P[i];
			}else if(_prjs_S2P[i].ty == 'i'){
				_poutS2P(0, _prjs_S2P[i]);
			}
		}
	}
	if(p){
		p.pt = (new Date()).getTime();
		p.ty = 'i';	
		p.log = p.log+ p.pt +' i:';
		_sAveS2P();
		_rNDrS2P();
		var t = _gPrEfS2P('time', 'int');
		if(t){
			var dt = (new Date()).getTime();
			if(t < dt){
				_sPrEfS2P('time', dt);
			}
		}	
		//close the edit form
		var d = $(_mPdS2P('efd'));
		d.style.display='none';			
	}
}
/**
	resets the total time worked, activity logs and activity id to current time.
*/
function _rSetS2P(pid){
	var flg = confirm("Are you sure you want to reset this activity's total hours ?");
	if(!flg) return;
	var p = _gPrjS2P(pid);
	if(p){
		p.tot = 0;
		p.ty = 'o';	
		p.id = (new Date()).getTime();
		p.pt = 0;
		p.log='';
		_sAveS2P();
		_rNDrS2P();
	}
}
/**
	removes the  from the list.
*/
function _RmvPS2P(pid){
	var flg = confirm("Are you sure you want to delete this activity?");
	if(!flg) return;
	if(_prjs_S2P && _prjs_S2P.length >0){
		for(var i=0; i < _prjs_S2P.length; i++){
			if(_prjs_S2P[i].id == pid){
				_prjs_S2P.splice(i,1); break;
			}
		}
		_sAveS2P();
		_rNDrS2P();
		//;
	}
}
function _EdPS2P(pid){
	var p = _gPrjS2P(pid);
	if(p){
		var frm = $(_mPdS2P('ef'));
		frm.pid.value=pid;
		frm.name.value = unescape(p.name);
		var hr = Math.floor(p.tot / 3600000);
		frm.hr.value = hr;
		var min = p.tot - (hr * 3600000);
		min = Math.floor(min / 60000);
		frm.min.value = min;
		var d = $(_mPdS2P('efd'));
		d.style.display='';	
		//;
	}
}
function _EdSPS2P(frm){
	var p = _gPrjS2P(frm.pid.value);
	if(p){
		var hr = parseInt(frm.hr.value);
		var min = parseInt(frm.min.value);
		if((hr || hr ==0) && (min || min==0)){
			var tot = (hr * 3600000) + (min * 60000);
			if(p.tot != tot){
				p.tot = tot;
				p.log = p.log + tot + ' e:';
			}
		}
		p.name = escape(frm.name.value);
		_sAveS2P();
		_rNDrS2P();
		//;
	}else{
		alert('Activity not found !!');
	}
	var d = $(_mPdS2P('efd'));
	d.style.display='none';
}
function _EdCS2P(){
		var d = $(_mPdS2P('efd'));
		d.style.display='none';	
		//;
}
/**
	_poutS2P - punch out function. This function updates the type, totaltime from last punch
	and the log.
*/
function _poutS2P(pid, p){
	if(!p){
		p = _gPrjS2P(pid);
	}
	if(p && p.ty != 'o'){
		var d = (new Date()).getTime()
		p.tot = p.tot + d - p.pt;
		p.pt=0;
		p.ty='o';
		p.log = p.log+ d + ' o:';
		_sAveS2P();
		_rNDrS2P();
		var t = _gPrEfS2P('time', 'int');
		if(t){
			var dt = (new Date()).getTime();
			if(t < dt){
				_sPrEfS2P('time', dt);
			}
		}
	}
}
function _tStRS2P(time){
	if(time){
		var hr = Math.floor(time / 3600000);
		time = time - (hr * 3600000);
		var min = Math.floor(time / 60000);	
		return (hr > 0 ?  (hr == 1? ''+hr+'&nbsp;hour': ''+hr+'&nbsp;hours')      : '') + ' '+ (min > 0 ?  (min == 1? ''+min+'&nbsp;min': ''+min+'&nbsp;mins')      : '0 min');
	}
	return "0 min";
}
function _mNthS2P(val){
	switch(val){
		case 0: return 'Jan';
		case 1: return 'Feb';
		case 2: return 'Mar';
		case 3: return 'Apr';
		case 4: return 'May';
		case 5: return 'Jun';
		case 6: return 'Jul';
		case 7: return 'Aug';
		case 8: return 'Sep';
		case 9: return 'Oct';
		case 10: return 'Nov';
		case 11: return 'Dec';
	}
}
function _gDDiS2P(time){
	var d = new Date(time);
	var t = " " + d.getDate() + "-" + _mNthS2P(d.getMonth()) + "-" + d.getFullYear();
	if(d.getHours() !=0 || d.getMinutes() !=0){
		t = t + (d.getHours() > 9 ? " "+d.getHours(): " 0"+ d.getHours()) + ":" + (d.getMinutes() > 9 ? d.getMinutes(): "0"+ d.getMinutes());
	}
	return t;
}
/**
	Render display every 1 minute.
*/
function _rNDrS2P(_f){
	clearTimeout(_tmr_S2P);
	_tmr_S2P = setTimeout("_rNDrS2P(true)", 60000);
	if(_prjs_S2P && _prjs_S2P.length > 0){
		var htm = '<table width="100%"><tr><th>My Activities</th></tr>';
		for(var i=0; i < _prjs_S2P.length; i++){
			var pid = _prjs_S2P[i].id;
			htm = htm + '<tr><td onmouseover="this.className=\'moScReEP\'" onmouseout="this.className=\'\'"><table width="100%" class="prjScReEP"><tr><td><span class="titleScReEP">';
			htm = htm + unescape(_prjs_S2P[i].name) + '</span></td><td align="right">';
			var timetext = '';
			if(_prjs_S2P[i].ty == 'i'){
				timetxt = _tStRS2P(_prjs_S2P[i].tot + ((new Date()).getTime() - _prjs_S2P[i].pt));
				htm = htm + '<button class="outScReEP" onclick="_poutS2P('+pid+')" ><b>OUT</b></button>';
			}else{
				timetxt = _tStRS2P(_prjs_S2P[i].tot);
				htm = htm + '<button class="inScReEP" onclick="_pinS2P('+pid+')" ><b>IN</b></button>';
			}
			htm = htm + '</td></tr><tr><td nowrap>';
			if(_prjs_S2P[i].ty == 'o')
				htm = htm + '<a href="/" class="smlScReEP" onclick="_EdPS2P('+pid+'); return false;">edit</a> &nbsp;<a href="/" class="smlScReEP" onclick="_lOgS2P('+pid+'); return false;">log</a> &nbsp;<a href="/" class="smlScReEP" onclick="_clRlGS2P('+pid+'); return false;">clear logs</a> &nbsp;<a href="/" class="smlScReEP" onclick="_RmvPS2P('+pid+'); return false;">remove</a> &nbsp;<a href="/" class="smlScReEP" onclick="_rSetS2P('+pid+'); return false;">reset</a><br/>';
			htm = htm + '<span class="greyScReEP">Started :'+_gDDiS2P(pid)+'</span><br/>';
			if(_prjs_S2P[i].ty == 'i'){
				htm = htm + '<span class="greyScReEP">IN time :'+_gDDiS2P(_prjs_S2P[i].pt)+'</span>';
			}
			htm = htm + '</td><td align="right"><strong>'+ timetxt + '</strong></td></tr></table></td></tr>';
		}
		htm = htm + "</table>";
		$(_mPdS2P('m')).innerHTML = htm;
		if(_f){
			//give time for the igoogle page to load.
			setTimeout("_PlaYS2P()", 3000);
		}
	}else{
		$(_mPdS2P('m')).innerHTML = '<i>No Activities</i>';
	}
}
function _PlaYS2P(){
	var time = _gPrEfS2P('time', 'int');
	var alarmMin = _gPrEfS2P('alarmMin', 'int');		
	if(alarmMin && time){
		var now = (new Date()).getTime();
		var elapsed = Math.floor((now - time)/60000);
		if( elapsed >=  alarmMin){
			var min = Math.floor(elapsed/alarmMin);
			if(min <=0) min =1;
			_sPrEfS2P('time', (time + (min * alarmMin * 60000)));
			var file = _gPrEfS2P('alarmVoice', 'str');
			if(!file) file = 'meng.wav';
			$(_mPdS2P('ply')).innerHTML = '<embed enablejavascript="false" hidden="true" width="0" height="0" autostart="true" name="_ALaS2P" id="_ALaS2P"  src="'+file+'"/>';
		}
	}
}
function _aDClsS2P(){
	$(_mPdS2P('msg')).style.display='none';
}
function _lOgS2P(pid){
	var p = _gPrjS2P(pid);
	if(p){
		var frm = $(_mPdS2P('lgf'));
		frm.pcS2PLog.value=p.log;
		frm.tTS2PTot.value=p.tot;
		frm.pJTS2PNmE.value=unescape(p.name);
		var d = new Date();
		frm.tMzS2POfs.value=d.getTimezoneOffset();
		frm.cRS2PTme.value=d.getTime();
		frm.submit();
	}else{
		alert('Sorry, activity not found !!');
	}
}
function _pjGrS2P(){
	if(_prjs_S2P && _prjs_S2P.length >0){
		var p = '';
		for(i=0; i< _prjs_S2P.length; i++){
			//remember the project name is escaped string
			p = p + _prjs_S2P[i].tot + ':' + _prjs_S2P[i].id + ':' + unescape(_prjs_S2P[i].name) + '>';
		}
		var frm = $(_mPdS2P('prf'));
		frm._prjs_S2P.value = p;
		d = new Date();
		frm.tMzS2POfs.value=d.getTimezoneOffset();
		frm.cRS2PTme.value=d.getTime();		
		frm.submit();
	}else{
		alert('Sorry, no activities found. Please add an activity');
	}
}
function _clRlGS2P(pid){
	var flg = confirm("Are you sure you want to clear logs for this activity?");
	if(!flg) return;
	var p = _gPrjS2P(pid);
	if(p){
		p.log = '';
		_sAveS2P();
	}
}
function _clRAlLS2P(){
	var flg = confirm("Are you sure you want to clear logs for all activities ?");
	if(!flg) return;
	if(_prjs_S2P && _prjs_S2P.length >0){
		for(i=0; i< _prjs_S2P.length; i++){
			_prjs_S2P[i].log = '';
		}
		_sAveS2P();
	}	
}
function _rStAlLS2P(){
	var flg = confirm("Are you sure you want to reset hours and logs for all activities ?");
	if(!flg) return;	
	if(_prjs_S2P && _prjs_S2P.length >0){
		for(i=0; i< _prjs_S2P.length; i++){
			var p = _prjs_S2P[i];
			p.tot = 0;
			p.ty = 'o';	
			p.id = (new Date()).getTime() + i;
			p.pt = 0;
			p.log='';
		}
		_sAveS2P();
		_rNDrS2P();
	}
}
function _alArOPS2P(){
	$(_mPdS2P('ald')).style.display = '';
}
function _alArCAS2P(){
	$(_mPdS2P('ald')).style.display = 'none';
}
function _alArSAS2P(frm){
	 _sPrEfS2P('alarmMin',frm._aMin.value);
	 _sPrEfS2P('alarmVoice',frm._aVoice.value);
	 _sPrEfS2P('time', (new Date()).getTime());
	 $(_mPdS2P('ald')).style.display = 'none';
}
//</script>